﻿ 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 笔画
    /// </summary>
    public class ExStroke : Stroke
    {
        private ImageSource imageSource;

        public ExStroke(StylusPointCollection stylusPointCollection, DrawingAttributes da) : base(stylusPointCollection)
        {
            if (DesignerProperties.GetIsInDesignMode(System.Windows.Application.Current.MainWindow))
                return;
            this.DrawingAttributes = da;
            var dv = new DrawingVisual();
            //  var size = 90;
            var img = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "res\\pen.png", UriKind.Absolute));



            var scalew = this.DrawingAttributes.Width / 20d;
            var scaleh = this.DrawingAttributes.Height / 20d;
            using (var conext = dv.RenderOpen())
            {
                //[关键]OpacityMask了解下？也许有童鞋想到的办法是，各种颜色的图片来一张？
                conext.PushOpacityMask(new ImageBrush(img));
                //用颜色生成画笔画一个矩形
                conext.DrawRectangle(new SolidColorBrush(this.DrawingAttributes.Color), null, new System.Windows.Rect(0, 0, img.Width * scalew, img.Height * scaleh));
                conext.Close();
            }
            var rtb = new RenderTargetBitmap((int)(img.Width * scalew), (int)(img.Height * scaleh), 96d, 96d, PixelFormats.Pbgra32);
            rtb.Render(dv);
            imageSource = BitmapFrame.Create(rtb);
            //[重要]此乃解决卡顿问题的关键！
            imageSource.Freeze();
        }


        protected override void DrawCore(DrawingContext dc, DrawingAttributes drawingAttributes)
        {


            var stylusPoints = this.StylusPoints;

            Point[] points = new Point[stylusPoints.Count];
            for (int i = 0; i < points.Length; i++)
            {
                points[i] = stylusPoints[i].ToPoint();
            }
            StrokeProcessor sp = new StrokeProcessor()
            {
                TendencyThreshold = 20,
                MinVectorLengthThreshold = 8
            };
            var sts = sp.Process(points);
            if (sts != null)
            {
                foreach (var item in sts)
                {
                    bool isleft = true;
                    bool isbottom = false;
                    if (item.Points != null && item.Points.Length > 1)
                    {
                        var fp = item.Points[0];
                        var lp = item.Points[item.Points.Length - 1];
                        isleft = lp.X < fp.X;//朝左
                        isbottom = lp.Y > fp.Y;//朝下
                    }
                    var ips = item.GetInterpolatedPoints(1);
                    var weights = item.GetWeights(ips.Length);

                    var downV = new Vector(0, 1);
                    for (int i = 0; i < ips.Length - 1; i++)//忽略最后一点
                    {
                        var p = ips[i];


                        var PressureFactor = weights[i];// pfs[pindex];  
                        var x = p.X - imageSource.Width / 2;
                        var y = p.Y - imageSource.Height / 2;

                        //  var Angle = Vector.AngleBetween(downV, ips[i + 1] - p);
                        double sx, sy;
                        if (item.Tendency == Tendency.B2T)
                        {
                            sx = p.X;
                            sy = p.Y;
                        }
                        else
                        {
                            sx = p.X + (isleft ? -imageSource.Width : imageSource.Width) / 2;
                            sy = p.Y + (isbottom ? -imageSource.Height : imageSource.Height) / 2;

                        }

                        //if (isleft)
                        //{
                        //    sx = p.X + imageSource.Width / 2;
                        //    sy = p.Y + imageSource.Height / 2;
                        //}
                        //else
                        //{
                        //    sx = p.X - imageSource.Width / 2;
                        //    sy = p.Y - imageSource.Height / 2;
                        //}
                        // dc.PushTransform(new RotateTransform(Angle,p.X, p.Y));

                        dc.PushTransform(new ScaleTransform(PressureFactor, PressureFactor, sx, sy));
                        // dc.PushTransform(new ScaleTransform(PressureFactor, PressureFactor, p.X, p.Y));
                        dc.DrawImage(imageSource, new System.Windows.Rect(x, y, imageSource.Width, imageSource.Height));
                        dc.Pop();
                        //  dc.Pop();
                    }
                }
            }



        }

    }

    public enum StylusType
    {
        /// <summary>
        /// 只有一个点
        /// </summary>
        None,
        /// <summary>
        /// 从左往右的横
        /// </summary>
        L2R,
        /// <summary>
        /// 从左下往右上的提
        /// </summary>
        LB2RT,
        /// <summary>
        /// 从上往下的竖
        /// </summary>
        T2B,
        /// <summary>
        /// 从右上到左下的撇
        /// </summary>
        RT2LB,
        /// <summary>
        /// 从左上到右下的捺
        /// </summary>
        LT2RB
    }
}